import { defineStore } from "pinia";
import { ref } from "vue";
import type { CreateOrderMedicinePay, OrderDetail, OrderPre } from "@/types/medicineTypes";
import { createMedicineOrderAPI, fetchMedicineOrderDetailAPI, fetchMedicineOrderPreAPI } from "@/api/medicineApi.ts";


const useMedicineStore = defineStore("medicineStore", () => {

    const medicineOrderPre = ref<OrderPre>(<OrderPre>{});

    const orderDetail = ref<OrderDetail>(<OrderDetail>{});

    const fetchMedicineOrderPre = async (prescriptionId: string): Promise<void> => {
        const { data }: { data: OrderPre } = await fetchMedicineOrderPreAPI(prescriptionId);
        medicineOrderPre.value = data;
    };

    const createMedicineOrder = async (params: CreateOrderMedicinePay): Promise<string> => {
        const { data } = await createMedicineOrderAPI(params);
        return data.id;
    };

    const fetchMedicineOrderDetail = async (id: string) => {
        const { data } = await fetchMedicineOrderDetailAPI(id);
        orderDetail.value=data
    };

    return {
        medicineOrderPre,
        orderDetail,
        fetchMedicineOrderPre,
        createMedicineOrder,
        fetchMedicineOrderDetail
    };
});

export default useMedicineStore;
